#include "support/base64.h"
#include "MapEditor.h"
#include "../DevInclude.h"


CMapEditor::CMapEditor(){
    m_pAStar=new MyAStar();
    m_pSceneToScene=new SceneToScene();
    m_pRoadData=new CRoadData();
}

CMapEditor::~CMapEditor(){
    SAFE_DEL(m_pAStar);
    SAFE_DEL(m_pSceneToScene);
    SAFE_DEL(m_pRoadData);
     
    
    clear();
}

void CMapEditor::clear(){
    gGridInfo.clear(); 
    gNpcInfo.clear();
    gMonsterInfo.clear();
    gJumpInfo.clear();
}

void CMapEditor::loadMap(string & cFilePath){
    clear();
    
   	unsigned long uSize = 0;
	std::string str = parseFilePath(cFilePath.c_str());
	string temp= CCFileUtils::sharedFileUtils()->fullPathForFilename(str.c_str());
	const char * pszFilePath =temp.c_str();
    unsigned char* pMapFileData = CCFileUtils::sharedFileUtils()->getFileData(pszFilePath, "rb", &uSize );
     
    unsigned char *pOut ;
    unsigned int outSize= base64Decode(pMapFileData, uSize, &pOut);
    
    if(outSize==0){
        return;
    }
    ByteBuffer reader; 
    reader.append( pOut,outSize);
     
    reader>>m_tMapInfo.nFileType;
    reader>>m_tMapInfo.nMapID;
    reader>>m_tMapInfo.nSceneID;
    reader>>m_tMapInfo.nResourceID;
    reader>>m_tMapInfo.nVersion;
    reader>>m_tMapInfo.nMapWidth;
    reader>>m_tMapInfo.nMapHeight;
    reader>>m_tMapInfo.nNpcCount;
    reader>>m_tMapInfo.nMonsterCount;
    reader>>m_tMapInfo.nLocations;
    reader>>m_tMapInfo.nJumpCount;
    
    
    int* blockMap;
    
    blockMap=new int[m_tMapInfo.nMapHeight*m_tMapInfo.nMapWidth];
    
    
   
    
    
     for (int i=0;i<m_tMapInfo.nMapHeight;i++){
        vector<GRID_INFO>  rowGrids ;
        gGridInfo.push_back(rowGrids);
         for (int j=0;j<m_tMapInfo.nMapWidth;j++){
            GRID_INFO info;
            reader>>info.nTileWalk;
            reader>>info.nTileFly;
            if (info.nTileWalk & GRID_BLOCK){
                blockMap[i*m_tMapInfo.nMapWidth+ j] = 1;
            } else {
                blockMap[i*m_tMapInfo.nMapWidth+ j] = 0; 
            };
            gGridInfo[i].push_back(info);
        }
    }
    
    m_pAStar->Init(blockMap, m_tMapInfo.nMapWidth, m_tMapInfo.nMapHeight);
    
    delete []blockMap;
  
    for (int i=0;i<m_tMapInfo.nNpcCount;i++){
        NPC_INFO info; 
        reader>>info.nDWId;
        reader>>info.nIndex;
        reader>>info.nFace;
        reader>>info.nPatrolRule;
        reader>>info.nPointCount;
        
        
        info.gPoints.clear();
        for (int j=0;j<info.nPointCount;j++){
            uint16 x,y,z;
            reader>>x;
            reader>>y;
            reader>>z;
            CCPoint3D pos;
            pos.setPoint(x, y, z);
            info.gPoints.push_back(pos);
        }
        
        
        gNpcInfo.push_back(info);
    }
    
    
    for (int i=0;i<m_tMapInfo.nMonsterCount;i++){
        NPC_INFO info;
        reader>>info.nDWId;
        reader>>info.nIndex;
        reader>>info.nFace;
        reader>>info.nPatrolRule;
        reader>>info.nPointCount;
        
        
        info.gPoints.clear();
        for (int j=0;j<info.nPointCount;j++){
            uint16 x,y,z;
            reader>>x;
            reader>>y;
            reader>>z;
            CCPoint3D pos;
            pos.setPoint(x, y, z);
            info.gPoints.push_back(pos);
        }
        
        
        gMonsterInfo.push_back(info);
    }
    
    
    
    for (int i=0;i<m_tMapInfo.nJumpCount;i++){
        JUMP_INFO info;
        reader>>info.nIndex;
        reader>>info.nXPos;
        reader>>info.nYPos;
        reader>>info.nZPos;
        reader>>info.nTransmitNum;
        
        
        info.gTransmitID.clear();
        for (int j=0;j<info.nTransmitNum;j++){
            uint32 tran;
            reader>>tran;
            info.gTransmitID.push_back(tran);
        }
        
        
        gJumpInfo.push_back(info);
    }
    
    delete [] pMapFileData;
    delete [] pOut;
}

 

int  CMapEditor::getPixelWidth(){
    return (((m_tMapInfo.nMapWidth *  DEFAULT_GRIDWIDTH) /NORMAL_BLOCK_WIDTH));
}

int  CMapEditor::getPixelHeight(){
    
    return (((m_tMapInfo.nMapHeight * (DEFAULT_GRIDHEIGHT * 0.5f)) / NORMAL_BLOCK_HEIGHT));
    
      
}

CCPoint CMapEditor::getIndexFromPos(float nX,float nY,float nWidth,float nHeight){
    if (nWidth == -1){
        nWidth = DEFAULT_GRIDWIDTH;
    };
    if (nHeight == -1){
        nHeight = DEFAULT_GRIDHEIGHT;
    };
    uint32 retX = 0;
    uint32 retY = 0;
    float comX = 0;
    float comY = 0;
    float halfWidth = float(nWidth / 2);
    float halfHeight = float(nHeight / 2);
    uint32 oX = (uint32((nY / nHeight)) * 2);
    uint32 oY = fmodf(nY , nHeight);
    if ((uint32((nX / halfWidth)) % 2) == 1){
        comX = (float((halfWidth - fmodf(nX ,halfWidth)) / halfWidth) * halfHeight);
        comY = (float(fmodf(nX , halfWidth) / halfWidth) * halfHeight);
    } else {
        if (fmodf(int(nX / halfWidth), 2 ) == 0){
            comX = (float(fmodf(nX ,halfWidth) / halfWidth) * halfHeight);
            comY = (float((halfWidth - fmodf(nX , halfWidth)) / halfWidth) * halfHeight);
        };
    };
    if (oY < comX){
        retX = oX;
        retY = (nX / nWidth);
    } else {
        if ((((oY >= comX)) && ((oY < (comX + (2 * comY)))))){
            retX = (oX + 1);
            if (nX < halfWidth){
                retY = 0;
            } else {
                retY = (float((nX - halfWidth) / nWidth) + 1);
            };
        } else {
            if ((((oY >= (comX + (2 * comY)))) && ((oY <= (((2 * comX) + (2 * comY)) + 3))))){
                retX = (oX + 2);
                retY = (nX / nWidth);
            };
        };
    };
    return ccp(retY,retX);
    
    
}

bool CMapEditor::getPosTranslucent(int nX, int nY, int nState){
    if (nState == -1){
        nState =  STATE_FLOOR;
    };
    CCPoint pos =getIndexFromPos(nX, nY);
    
    if(pos.y>=gGridInfo.size() ||  pos.x >=gGridInfo[pos.y].size()  ){
        return (true);
    };
    
    uint32 tempX = nX;
    uint32 tempY = nY;
    if (fmod(pos.y, 2) == 0){
        tempX = (tempX - (pos.x * DEFAULT_GRIDWIDTH));
        tempY = (tempY - (((uint32((pos.y/ 2)) - 1) * DEFAULT_GRIDHEIGHT) + (DEFAULT_GRIDHEIGHT / 2)));
    } else {
        tempX = (tempX - (((pos.x - 1) * DEFAULT_GRIDWIDTH) + (DEFAULT_GRIDWIDTH / 2)));
        tempY = (tempY - (uint32((pos.y/ 2)) * DEFAULT_GRIDHEIGHT));
    };
    CCPoint fPos = getIndexFromPos(tempX, tempY, (DEFAULT_GRIDWIDTH / 2), (DEFAULT_GRIDHEIGHT / 2));
    GRID_INFO gridInfo= (gGridInfo[pos.y])[pos.x];
    if ((((fPos.y == 2)) && ((fPos.x == 0)))){
        if (nState == STATE_FLOOR){
            return (!(((gridInfo.nTileWalk & GRID_TRANSLUCENT1) == 0)));
        };
        if (nState == STATE_SKY){
            return (!(((gridInfo.nTileFly & GRID_TRANSLUCENT1) == 0)));
        };
    } else {
        if ((((fPos.y == 1)) && ((fPos.x == 1)))){
            if (nState == STATE_FLOOR){
                return (!(((gridInfo.nTileWalk & GRID_TRANSLUCENT2) == 0)));
            };
            if (nState == STATE_SKY){
                return (!(((gridInfo.nTileFly & GRID_TRANSLUCENT2) == 0)));
            };
        } else {
            if ((((fPos.y == 2)) && ((fPos.x == 1)))){
                if (nState == STATE_FLOOR){
                    return (!(((gridInfo.nTileWalk & GRID_TRANSLUCENT3) == 0)));
                };
                if (nState == STATE_SKY){
                    return (!(((gridInfo.nTileFly & GRID_TRANSLUCENT3) == 0)));
                };
            } else {
                if ((((fPos.y == 3)) && ((fPos.x == 1)))){
                    if (nState == STATE_FLOOR){
                        return (!(((gridInfo.nTileWalk & GRID_TRANSLUCENT4) == 0)));
                    };
                    if (nState == STATE_SKY){
                        return (!(((gridInfo.nTileFly & GRID_TRANSLUCENT4) == 0)));
                    };
                };
            };
        };
    };
    return (false);
}






bool CMapEditor::isArrivalble(int nX,int nY,int nZ){
    if ((((((((nX < 0)) || ((nY < 0)))) || ((nY >= gGridInfo.size())))) || ((nX>= gGridInfo[nY].size())))){
        return (false);
    };
  
    if (nZ == -1){
        nZ = STATE_FLOOR;
    };
    if (nZ == STATE_FLOOR){
        return (((!(((gGridInfo[nY])[nX].nTileWalk & GRID_BLOCK))) && (!(((gGridInfo[nY])[nX].nTileWalk & GRID_BLOCKSIGHT)))));
    };
    if (nZ == STATE_SKY){
        return (((!(((gGridInfo[nY])[nX].nTileFly & GRID_BLOCK))) && (!(((gGridInfo[nY])[nX].nTileFly & GRID_BLOCKSIGHT)))));
    };
    return (false);
}


CCPoint CMapEditor::getAppropriateIndex( const CCPoint& targetPos,  const CCPoint& pos, uint32 nBlockNum, bool flag, int nZ){
    
    if (nZ == -1){
        nZ = STATE_FLOOR;
    };
    CCPoint tPos = getIndexFromPos(targetPos.x, targetPos.y);
    CCPoint fPos = getIndexFromPos(pos.x, pos.y);
    if ((((tPos.x == fPos.x)) && ((tPos.y == fPos.y)))){
        return (fPos);
    };
    if (((flag) && (isArrivalble(fPos.x, fPos.y, nZ)))){
        return (fPos);
    };
    if ((((tPos.x > fPos.x)) && ((((((((((fmod(tPos.x,2) == 0)) && ((fmod(fPos.x,2) == 0)))) && ((tPos.y < fPos.y)))) || ((((((fmod(tPos.x,2) == 0)) && ((fmod(fPos.x,2) == 1)))) && ((tPos.y <= fPos.y)))))) || ((((fmod(tPos.x,2) == 1)) && ((tPos.y < fPos.y)))))))){
        return (findFromSouthWest(fPos, nZ, nBlockNum));
    };
    if ((((tPos.x == fPos.x)) && ((tPos.y < fPos.y)))){
        return (findFromWest(fPos, nZ, nBlockNum));
    };
    if ((((tPos.x < fPos.x)) && ((((((((((fmod(tPos.x,2) == 0)) && ((fmod(fPos.x,2) == 0)))) && ((tPos.y < fPos.y)))) || ((((((fmod(tPos.x,2) == 0)) && ((fmod(fPos.x,2) == 1)))) && ((tPos.y <= fPos.y)))))) || ((((fmod(tPos.x,2) == 1)) && ((tPos.y < fPos.y)))))))){
        return (findFromNorthWest(fPos, nZ, nBlockNum));
    };
    if ((((tPos.y == fPos.y)) && ((tPos.x < fPos.x)))){
        return (findFromNorth(fPos, nZ, nBlockNum));
    };
    if ((((tPos.x < fPos.x)) && ((((((((((fmod(tPos.x,2) == 1)) && ((fmod(fPos.x,2) == 0)))) && ((tPos.y >= fPos.y)))) || ((((((fmod(tPos.x,2) == 1)) && ((fmod(fPos.x,2) == 1)))) && ((tPos.y > fPos.y)))))) || ((((fmod(tPos.x,2) == 0)) && ((tPos.y > fPos.y)))))))){
        return (findFromNorthEast(fPos, nZ, nBlockNum));
    };
    if ((((tPos.x == fPos.x)) && ((tPos.y > fPos.y)))){
        return (findFromEast(fPos, nZ, nBlockNum));
    };
    if ((((tPos.x > fPos.x)) && ((((((((((fmod(tPos.x,2) == 1)) && ((fmod(fPos.x,2) == 0)))) && ((tPos.y >= fPos.y)))) || ((((((fmod(tPos.x,2) == 1)) && ((fmod(fPos.x,2) == 1)))) && ((tPos.y > fPos.y)))))) || ((((fmod(tPos.x,2) == 0)) && ((tPos.y > fPos.y)))))))){
        return (findFromSouthEast(fPos, nZ, nBlockNum));
    };
    if ((((tPos.x > fPos.x)) && ((tPos.y == fPos.y)))){
        return (findFromSouth(fPos, nZ, nBlockNum));
    };
    return (ccp(-1, -1));
}




CCPoint CMapEditor::findFromSouthWest( CCPoint pos, uint32 nZ, uint32 nBlockNum){
    CCPoint _local6;
    CCPoint _local7;
    int j=0;
    CCPoint _local9;
    std::list<CCPoint> gFlagPos;
   int i=1;
    while (i <= nBlockNum) {
        pos = SOUTHWEST(pos);
        if (isArrivalble(pos.x, pos.y, nZ)){
            return (pos);
        };
        _local6 = pos;
        _local7 = pos;
        j = 0;
        while (j < i) {
            gFlagPos.clear();
            _local6 = NORTHWEST(_local6);
            if (isArrivalble(_local6.x, _local6.y, nZ)){
                return (_local6);
            };
            gFlagPos.push_back(ccp(_local6.x, _local6.y));
            _local7 = SOUTHEAST(_local7);
            if (isArrivalble(_local7.x, _local7.y, nZ)){
                return (_local7);
            };
            gFlagPos.push_back(ccp(_local7.x, _local7.y));
            j++;
        };
        if (gFlagPos.size() != 2){
            return (ccp(-1, -1));
        };
        j = 0;
        _local7 = gFlagPos.back();
        gFlagPos.pop_back();
        _local6 = gFlagPos.back();
        gFlagPos.pop_back();
        while (j < (i * 2)) {
            gFlagPos.clear();
            _local6 = NORTHEAST(_local6);
            if (isArrivalble(_local6.x, _local6.y, nZ)){
                return (_local6);
            };
            gFlagPos.push_back(ccp(_local6.x, _local6.y));
            _local7 = NORTHEAST(_local7);
            if (isArrivalble(_local7.x, _local7.y, nZ)){
                return (_local7);
            };
            gFlagPos.push_back(ccp(_local7.x, _local7.y));
            j++;
        };
        if (gFlagPos.size() != 2){
            return (ccp(-1, -1));
        };
        j = 0;
        _local7 = gFlagPos.back();
        gFlagPos.pop_back();
        _local6 = gFlagPos.back();
        gFlagPos.pop_back();
        while (j < i) {
            _local6 = SOUTHEAST(_local6);
            if (isArrivalble(_local6.x, _local6.y, nZ)){
                return (_local6);
            };
            _local7 = NORTHWEST(_local7);
            if (isArrivalble(_local7.x, _local7.y, nZ)){
                return (_local7);
            };
            j++;
        };
        _local9 = NORTHEAST(pos, (i * 2));
        if (isArrivalble(_local9.x, _local9.y, nZ)){
            return (_local9);
        };
        i++;
    };
    return (ccp(-1, -1));
}

CCPoint CMapEditor::findFromWest( CCPoint pos, uint32 nZ, uint32 nBlockNum){
    CCPoint _local6;
    CCPoint _local7;
    int j=0;
    CCPoint _local9;
    std::list<CCPoint> gFlagPos;
   int i=1;
    while (i <= nBlockNum) {
        pos = WEST(pos);
        if (isArrivalble(pos.x, pos.y, nZ)){
            return (pos);
        };
        _local6 = pos;
        _local7 = pos;
        j = 0;
        while (j < (i * 2)) {
            gFlagPos.clear();
            _local6 = NORTHEAST(_local6);
            if (isArrivalble(_local6.x, _local6.y, nZ)){
                return (_local6);
            };
            gFlagPos.push_back(ccp(_local6.x, _local6.y));
            _local7 = SOUTHEAST(_local7);
            if (isArrivalble(_local7.x, _local7.y, nZ)){
                return (_local7);
            };
            gFlagPos.push_back(ccp(_local7.x, _local7.y));
            j++;
        };
        if (gFlagPos.size() != 2){
            return (ccp(-1, -1));
        };
        j = 0;
        _local7 = gFlagPos.back();
        gFlagPos.pop_back();
        _local6 = gFlagPos.back();
        gFlagPos.pop_back();
        while (j < ((2 * i) - 1)) {
            gFlagPos.clear();
            _local6 = SOUTHEAST(_local6);
            if (isArrivalble(_local6.x, _local6.y, nZ)){
                return (_local6);
            };
            gFlagPos.push_back(ccp(_local6.x, _local6.y));
            _local7 = NORTHEAST(_local7);
            if (isArrivalble(_local7.x, _local7.y, nZ)){
                return (_local7);
            };
            gFlagPos.push_back(ccp(_local7.x, _local7.y));
            j++;
        };
        _local9 = EAST(pos, (i * 2));
        if (isArrivalble(_local9.x, _local9.y, nZ)){
            return (_local9);
        };
        i++;
    };
    return (ccp(-1, -1));
}

CCPoint CMapEditor::findFromNorthWest(  CCPoint pos, uint32 nZ, uint32 nBlockNum){
    CCPoint _local6;
    CCPoint _local7;
    int j=0;
    CCPoint _local9;
    std::list<CCPoint> gFlagPos;
   int i=1;
    while (i <= nBlockNum) {
        pos = NORTHWEST(pos);
        if (isArrivalble(pos.x, pos.y, nZ)){
            return (pos);
        };
        _local6 = pos;
        _local7 = pos;
        j = 0;
        while (j < i) {
            gFlagPos.clear();
            _local6 = NORTHEAST(_local6);
            if (isArrivalble(_local6.x, _local6.y, nZ)){
                return (_local6);
            };
            gFlagPos.push_back(ccp(_local6.x, _local6.y));
            _local7 = SOUTHWEST(_local7);
            if (isArrivalble(_local7.x, _local7.y, nZ)){
                return (_local7);
            };
            gFlagPos.push_back(ccp(_local7.x, _local7.y));
            j++;
        };
        if (gFlagPos.size() != 2){
            return (ccp(-1, -1));
        };
        j = 0;
        _local7 = gFlagPos.back();
        gFlagPos.pop_back();
        _local6 = gFlagPos.back();
        gFlagPos.pop_back();
        while (j < (i * 2)) {
            gFlagPos.clear();
            _local6 = SOUTHEAST(_local6);
            if (isArrivalble(_local6.x, _local6.y, nZ)){
                return (_local6);
            };
            gFlagPos.push_back(ccp(_local6.x, _local6.y));
            _local7 = SOUTHEAST(_local7);
            if (isArrivalble(_local7.x, _local7.y, nZ)){
                return (_local7);
            };
            gFlagPos.push_back(ccp(_local7.x, _local7.y));
            j++;
        };
        if (gFlagPos.size() != 2){
            return (ccp(-1, -1));
        };
        j = 0;
        _local7 = gFlagPos.back();
        gFlagPos.pop_back();
        _local6 = gFlagPos.back();
        gFlagPos.pop_back();
        while (j < i) {
            _local6 = SOUTHWEST(_local6);
            if (isArrivalble(_local6.x, _local6.y, nZ)){
                return (_local6);
            };
            _local7 = NORTHEAST(_local7);
            if (isArrivalble(_local7.x, _local7.y, nZ)){
                return (_local7);
            };
            j++;
        };
        _local9 = SOUTHEAST(pos, (i * 2));
        if (isArrivalble(_local9.x, _local9.y, nZ)){
            return (_local9);
        };
        i++;
    };
    return (ccp(-1, -1));
}

CCPoint CMapEditor::findFromNorth( CCPoint pos, uint32 nZ, uint32 nBlockNum){
    CCPoint _local6;
    CCPoint _local7;
    int j=0;
    CCPoint _local9;
    std::list<CCPoint> gFlagPos;
   int i=1;
    while (i <= nBlockNum) {
        pos = NORTH(pos);
        if (isArrivalble(pos.x, pos.y, nZ)){
            return (pos);
        };
        _local6 = pos;
        _local7 = pos;
        j = 0;
        while (j < (i * 2)) {
            gFlagPos.clear();
            _local6 = SOUTHEAST(_local6);
            if (isArrivalble(_local6.x, _local6.y, nZ)){
                return (_local6);
            };
            gFlagPos.push_back(ccp(_local6.x, _local6.y));
            _local7 = SOUTHWEST(_local7);
            if (isArrivalble(_local7.x, _local7.y, nZ)){
                return (_local7);
            };
            gFlagPos.push_back(ccp(_local7.x, _local7.y));
            j++;
        };
        if (gFlagPos.size() != 2){
            return (ccp(-1, -1));
        };
        j = 0;
        _local7 = gFlagPos.back();
        gFlagPos.pop_back();
        _local6 = gFlagPos.back();
        gFlagPos.pop_back();
        while (j < ((2 * i) - 1)) {
            gFlagPos.clear();
            _local6 = SOUTHWEST(_local6);
            if (isArrivalble(_local6.x, _local6.y, nZ)){
                return (_local6);
            };
            gFlagPos.push_back(ccp(_local6.x, _local6.y));
            _local7 = SOUTHEAST(_local7);
            if (isArrivalble(_local7.x, _local7.y, nZ)){
                return (_local7);
            };
            gFlagPos.push_back(ccp(_local7.x, _local7.y));
            j++;
        };
        _local9 = SOUTH(pos, (i * 2));
        if (isArrivalble(_local9.x, _local9.y, nZ)){
            return (_local9);
        };
        i++;
    };
    return (ccp(-1, -1));
}

CCPoint CMapEditor::findFromNorthEast(  CCPoint pos, uint32 nZ, uint32 nBlockNum){
    CCPoint _local6;
    CCPoint _local7;
    int j=0;
    CCPoint _local9;
    std::list<CCPoint> gFlagPos;
   int i=1;
    while (i <= nBlockNum) {
        pos = NORTHEAST(pos);
        if (isArrivalble(pos.x, pos.y, nZ)){
            return (pos);
        };
        _local6 = pos;
        _local7 = pos;
        j = 0;
        while (j < i) {
            gFlagPos.clear();
            _local6 = SOUTHEAST(_local6);
            if (isArrivalble(_local6.x, _local6.y, nZ)){
                return (_local6);
            };
            gFlagPos.push_back(ccp(_local6.x, _local6.y));
            _local7 = NORTHWEST(_local7);
            if (isArrivalble(_local7.x, _local7.y, nZ)){
                return (_local7);
            };
            gFlagPos.push_back(ccp(_local7.x, _local7.y));
            j++;
        };
        if (gFlagPos.size() != 2){
            return (ccp(-1, -1));
        };
        j = 0;
        _local7 = gFlagPos.back();
        gFlagPos.pop_back();
        _local6 = gFlagPos.back();
        gFlagPos.pop_back();
        while (j < (i * 2)) {
            gFlagPos.clear();
            _local6 = SOUTHWEST(_local6);
            if (isArrivalble(_local6.x, _local6.y, nZ)){
                return (_local6);
            };
            gFlagPos.push_back(ccp(_local6.x, _local6.y));
            _local7 = SOUTHWEST(_local7);
            if (isArrivalble(_local7.x, _local7.y, nZ)){
                return (_local7);
            };
            gFlagPos.push_back(ccp(_local7.x, _local7.y));
            j++;
        };
        if (gFlagPos.size() != 2){
            return (ccp(-1, -1));
        };
        j = 0;
        _local7 = gFlagPos.back();
        gFlagPos.pop_back();
        _local6 = gFlagPos.back();
        gFlagPos.pop_back();
        while (j < i) {
            _local6 = NORTHWEST(_local6);
            if (isArrivalble(_local6.x, _local6.y, nZ)){
                return (_local6);
            };
            _local7 = SOUTHEAST(_local7);
            if (isArrivalble(_local7.x, _local7.y, nZ)){
                return (_local7);
            };
            j++;
        };
        _local9 = SOUTHWEST(pos, (i * 2));
        if (isArrivalble(_local9.x, _local9.y, nZ)){
            return (_local9);
        };
        i++;
    };
    return (ccp(-1, -1));
}

CCPoint CMapEditor::findFromEast( CCPoint pos, uint32 nZ, uint32 nBlockNum){
    CCPoint _local6;
    CCPoint _local7;
    int j=0;
    CCPoint _local9;
    std::list<CCPoint> gFlagPos;
   int i=1;
    while (i <= nBlockNum) {
        pos = EAST(pos);
        if (isArrivalble(pos.x, pos.y, nZ)){
            return (pos);
        };
        _local6 = pos;
        _local7 = pos;
        j = 0;
        while (j < (i * 2)) {
            gFlagPos.clear();
            _local6 = SOUTHWEST(_local6);
            if (isArrivalble(_local6.x, _local6.y, nZ)){
                return (_local6);
            };
            gFlagPos.push_back(ccp(_local6.x, _local6.y));
            _local7 = NORTHWEST(_local7);
            if (isArrivalble(_local7.x, _local7.y, nZ)){
                return (_local7);
            };
            gFlagPos.push_back(ccp(_local7.x, _local7.y));
            j++;
        };
        if (gFlagPos.size() != 2){
            return (ccp(-1, -1));
        };
        j = 0;
        _local7 = gFlagPos.back();
        gFlagPos.pop_back();
        _local6 = gFlagPos.back();
        gFlagPos.pop_back();
        while (j < ((2 * i) - 1)) {
            gFlagPos.clear();
            _local6 = NORTHWEST(_local6);
            if (isArrivalble(_local6.x, _local6.y, nZ)){
                return (_local6);
            };
            gFlagPos.push_back(ccp(_local6.x, _local6.y));
            _local7 = SOUTHWEST(_local7);
            if (isArrivalble(_local7.x, _local7.y, nZ)){
                return (_local7);
            };
            gFlagPos.push_back(ccp(_local7.x, _local7.y));
            j++;
        };
        _local9 = WEST(pos, (i * 2));
        if (isArrivalble(_local9.x, _local9.y, nZ)){
            return (_local9);
        };
        i++;
    };
    return (ccp(-1, -1));
}

CCPoint CMapEditor::findFromSouthEast( CCPoint pos, uint32 nZ, uint32 nBlockNum){
    CCPoint _local6;
    CCPoint _local7;
    int j=0;
    CCPoint _local9;
    std::list<CCPoint> gFlagPos;
    int i=1;
    while (i <= nBlockNum) {
        pos = SOUTHEAST(pos);
        if (isArrivalble(pos.x, pos.y, nZ)){
            return (pos);
        };
        _local6 = pos;
        _local7 = pos;
        j = 0;
        while (j < i) {
            gFlagPos.clear();
            _local6 = SOUTHWEST(_local6);
            if (isArrivalble(_local6.x, _local6.y, nZ)){
                return (_local6);
            };
            gFlagPos.push_back(ccp(_local6.x, _local6.y));
            _local7 = NORTHEAST(_local7);
            if (isArrivalble(_local7.x, _local7.y, nZ)){
                return (_local7);
            };
            gFlagPos.push_back(ccp(_local7.x, _local7.y));
            j++;
        };
        if (gFlagPos.size() != 2){
            return (ccp(-1, -1));
        };
        j = 0;
        _local7 = gFlagPos.back();
        gFlagPos.pop_back();
        _local6 = gFlagPos.back();
        gFlagPos.pop_back();
        while (j < (i * 2)) {
            gFlagPos.clear();
            _local6 = NORTHWEST(_local6);
            if (isArrivalble(_local6.x, _local6.y, nZ)){
                return (_local6);
            };
            gFlagPos.push_back(ccp(_local6.x, _local6.y));
            _local7 = NORTHWEST(_local7);
            if (isArrivalble(_local7.x, _local7.y, nZ)){
                return (_local7);
            };
            gFlagPos.push_back(ccp(_local7.x, _local7.y));
            j++;
        };
        if (gFlagPos.size() != 2){
            return (ccp(-1, -1));
        };
        j = 0;
        _local7 = gFlagPos.back();
        gFlagPos.pop_back();
        _local6 = gFlagPos.back();
        gFlagPos.pop_back();
        while (j < i) {
            _local6 = NORTHEAST(_local6);
            if (isArrivalble(_local6.x, _local6.y, nZ)){
                return (_local6);
            };
            _local7 = SOUTHWEST(_local7);
            if (isArrivalble(_local7.x, _local7.y, nZ)){
                return (_local7);
            };
            j++;
        };
        _local9 = NORTHWEST(pos, (i * 2));
        if (isArrivalble(_local9.x, _local9.y, nZ)){
            return (_local9);
        };
        i++;
    };
    return (ccp(-1, -1));
}

CCPoint CMapEditor::findFromSouth( CCPoint pos, uint32 nZ, uint32 nBlockNum){
    CCPoint _local6;
    CCPoint _local7;
    int j=0;
    CCPoint _local9;
    std::list<CCPoint> gFlagPos;
   int i=1;
    while (i <= nBlockNum) {
        pos = SOUTH(pos);
        if (isArrivalble(pos.x, pos.y, nZ)){
            return (pos);
        };
        _local6 = pos;
        _local7 = pos;
        j = 0;
        while (j < (i * 2)) {
            gFlagPos.clear();
            _local6 = NORTHWEST(_local6);
            if (isArrivalble(_local6.x, _local6.y, nZ)){
                return (_local6);
            };
            gFlagPos.push_back(ccp(_local6.x, _local6.y));
            _local7 = NORTHEAST(_local7);
            if (isArrivalble(_local7.x, _local7.y, nZ)){
                return (_local7);
            };
            gFlagPos.push_back(ccp(_local7.x, _local7.y));
            j++;
        };
        if (gFlagPos.size() != 2){
            return (ccp(-1, -1));
        };
        j = 0;
        _local7 = gFlagPos.back();
        gFlagPos.pop_back();
        _local6 = gFlagPos.back();
        gFlagPos.pop_back();
        while (j < ((2 * i) - 1)) {
            gFlagPos.clear();
            _local6 = NORTHEAST(_local6);
            if (isArrivalble(_local6.x, _local6.y, nZ)){
                return (_local6);
            };
            gFlagPos.push_back(ccp(_local6.x, _local6.y));
            _local7 = NORTHWEST(_local7);
            if (isArrivalble(_local7.x, _local7.y, nZ)){
                return (_local7);
            };
            gFlagPos.push_back(ccp(_local7.x, _local7.y));
            j++;
        };
        _local9 = NORTH(pos, (i * 2));
        if (isArrivalble(_local9.x, _local9.y, nZ)){
            return (_local9);
        };
        i++;
    };
    return (ccp(-1, -1));
}


CCPoint CMapEditor::SOUTHWEST(const CCPoint& pos, uint32 nNum){
    CCPoint ret;
    CCPoint fromPos(pos.x, pos.y);
    int i=0;
    while (i < nNum) {
        if (fmod(fromPos.x , 2) == 0){
            ret.x = (fromPos.x + 1);
            ret.y = fromPos.y;
        } else {
            if ((((fmod(fromPos.x , 2) == 1)) || ((fmod(fromPos.x , 2) == -1)))){
                ret.x = (fromPos.x + 1);
                ret.y = (fromPos.y - 1);
            };
        };
        fromPos.x = ret.x;
        fromPos.y = ret.y;
        i++;
    };
    return (ret);
}
CCPoint CMapEditor::WEST(const CCPoint& pos, uint32 nNum){
    return ccp(pos.x, (pos.y - nNum));
      
}
CCPoint CMapEditor::NORTHWEST(const CCPoint& pos, uint32 nNum){
    CCPoint ret;
    CCPoint fromPos(pos.x, pos.y);
    int i=0;
    while (i < nNum) {
        if (fmod(fromPos.x , 2) == 0){
            ret.x = (fromPos.x - 1);
            ret.y = fromPos.y;
        } else {
            if ((((fmod(fromPos.x , 2) == 1)) || ((fmod(fromPos.x , 2) == -1)))){
                ret.x = (fromPos.x - 1);
                ret.y = (fromPos.y - 1);
            };
        };
        fromPos.x = ret.x;
        fromPos.y = ret.y;
        i++;
    };
    return (ret);
}
CCPoint CMapEditor::NORTH(const CCPoint& pos, uint32 nNum){
    return ccp((pos.x - (2 * nNum)), pos.y);
}

CCPoint CMapEditor::NORTHEAST(const CCPoint& pos, uint32 nNum){
    CCPoint ret;
    CCPoint fromPos(pos.x, pos.y);
    int i=0;
    while (i < nNum) {
        if (fmod(fromPos.x , 2) == 0){
            ret.x = (fromPos.x - 1);
            ret.y = (fromPos.y + 1);
        } else {
            if ((((fmod(fromPos.x , 2) == 1)) || ((fmod(fromPos.x , 2) == -1)))){
                ret.x = (fromPos.x - 1);
                ret.y = fromPos.y;
            };
        };
        fromPos.x = ret.x;
        fromPos.y = ret.y;
        i++;
    };
    return (ret);
}

CCPoint CMapEditor::EAST(const CCPoint& pos, uint32 nNum){ 
    return ccp(pos.x, (pos.y + nNum));
}

CCPoint CMapEditor::SOUTHEAST(const CCPoint& pos, uint32 nNum){
    CCPoint ret;
    CCPoint fromPos(pos.x, pos.y);
    int i=0;
    while (i < nNum) {
        if (fmod(fromPos.x , 2) == 0){
            ret.x = (fromPos.x + 1);
            ret.y = (fromPos.y + 1);
        } else {
            if ((((fmod(fromPos.x , 2) == 1)) || ((fmod(fromPos.x , 2) == -1)))){
                ret.x = (fromPos.x + 1);
                ret.y = fromPos.y;
            };
        };
        fromPos.x = ret.x;
        fromPos.y = ret.y;
        i++;
    };
    return (ret);
}
CCPoint CMapEditor::SOUTH(const CCPoint& pos, uint32 nNum){ 
    return ccp((pos.x + (2 * nNum)), pos.y);
}